package com.masterclient.app.modules.goods.contract;
import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.OperateResultBean;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.cart.beans.AddShopCartBean;
import com.eoner.baselibrary.bean.goods.CommodityDetailBean;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;

import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class CommodityDetailPresenter extends CommodityDetailContract.Presenter{

    @Override
    public void getCommodityDetail(@android.support.annotation.NonNull String id,String code,boolean is_form_dealer) {
        Observable<CommodityDetailBean> observable = RetrofitUtil2.getInstance().getCommodityDetail(id,code,is_form_dealer?"1":"0");
        Disposable disposable = toSubscribe(observable, new Consumer<CommodityDetailBean>() {
            @Override
            public void accept(@NonNull CommodityDetailBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showCommodityDetail(bean.getData());
                }else {
                    mView.getCommodityDetailFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void pickUpSalesrule(String coupon_code,String parent_product_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("coupon_code",coupon_code);
            jsonObject.put("parent_product_id",parent_product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = RetrofitUtil2.getInstance().coupon_receive(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if ("0".equals(bean.getCode())){
                    mView.pickUpSalesruleSuccess(coupon_code,bean.getMsg());
                }else {
                    mView.pickUpSalesruleFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.pickUpSalesruleFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void removeFavorite(String product_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("product_id",product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = RetrofitUtil2.getInstance().removeFavorite(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.removeFavoriteSuccess();
                }else{
                    mView.favoriteFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.favoriteFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void addFavorite(String product_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("product_id",product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = RetrofitUtil2.getInstance().favoriteAdd(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.addFavoriteSuccess();
                }else{
                    mView.favoriteFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.favoriteFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void addCart(String product_id, String num) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("product_id",product_id);
            jsonObject.put("num",num);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<AddShopCartBean> observable = RetrofitUtil2.getInstance().addShopCart(body);
        Disposable disposable = toSubscribe(observable, new Consumer<AddShopCartBean>() {
            @Override
            public void accept(@NonNull AddShopCartBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.addCartSuccess(bean);
                }else{
                    mView.addCartFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.addCartFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void confirm_order(String buy_product, String buy_type, String sale_type) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject buyProduct = new JSONObject(buy_product);
            jsonObject.put("buy_product",buyProduct);
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("sale_type",sale_type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if("0".equals(bean.getCode()) || "20001".equals(bean.getCode())){
                    mView.confirm_order_success(bean.getData(),bean.getCode());
                }else{
                    mView.confirm_order_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.confirm_order_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void setSaleRemain(String event_map_id, String product_id, String type) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().setSaleRemain(event_map_id,product_id,type), (Consumer<OperateResultBean>) bean -> {
            if ("0".equals(bean.getCode())){
                mView.setSaleRemainSuccess();
            }else {
                mView.setSaleRemainFail(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.setSaleRemainFail("网络异常，请重试");
        }));
    }

}